﻿/*
 * @author: wizardc
 */

using Dou.Resource;
using UnityEngine;

namespace Dou.Audio
{
    /// <summary>
    /// 音频控制器
    /// </summary>
    [RequireComponent(typeof(AudioSource))]
    public class AudioController : MonoBehaviour
    {
        private AudioSource _audioSource;
        
        private string _assetName;
        private bool _isPlaying = false;
        private float _time = 0;
        
        public AudioSource audioSource
        {
            get
            {
                if (_audioSource == null)
                {
                    _audioSource = GetComponent<AudioSource>();
                }
                return _audioSource;
            }
        }
        
        /// <summary>
        /// 设置的 AudioClip 资源名称
        /// </summary>
        public string assetName
        {
            set
            {
                if (_assetName == value)
                {
                    return;
                }
                if (!string.IsNullOrEmpty(_assetName))
                {
                    _isPlaying = false;
                    audioSource.Stop();
                    audioSource.clip = null;
                    AssetManager.instance.UnloadAsset(_assetName);
                }
                _assetName = value;
                if (!string.IsNullOrEmpty(_assetName))
                {
                    AssetManager.instance.LoadAsset(_assetName, asset =>
                    {
                        if (_assetName != value)
                        {
                            AssetManager.instance.UnloadAsset(_assetName);
                            return;
                        }
                        audioSource.clip = asset as AudioClip;
                        if (_isPlaying)
                        {
                            audioSource.time = _time;
                            audioSource.Play();
                        }
                    });
                }
            }
            get => _assetName;
        }
        
        /// <summary>
        /// 是否播放
        /// </summary>
        public bool isPlaying => _isPlaying;

        /// <summary>
        /// 是否静音
        /// </summary>
        public bool mute
        {
            set => audioSource.mute = value;
            get => audioSource.mute;
        }

        /// <summary>
        /// 音量
        /// </summary>
        public float volume
        {
            set => audioSource.volume = value;
            get => audioSource.volume;
        }
        
        /// <summary>
        /// 开始播放
        /// </summary>
        public void Play(bool isLoop, float time = 0)
        {
            _isPlaying = true;
            _time = time;
            audioSource.loop = isLoop;
            if (audioSource.clip != null)
            {
                audioSource.time = _time;
                audioSource.Play();
            }
        }

        /// <summary>
        /// 暂停
        /// </summary>
        public void Pause()
        {
            _isPlaying = false;
            audioSource.Pause();
        }

        /// <summary>
        /// 恢复播放
        /// </summary>
        public void Resume()
        {
            _isPlaying = true;
            audioSource.UnPause();
        }

        /// <summary>
        /// 停止播放
        /// </summary>
        public void Stop()
        {
            _isPlaying = false;
            audioSource.Stop();
        }
    }
}
